package Tagger;

import java.io.*;
import java.util.*;

import DataObjects.*;
import Main.*;

public class MalletTagger implements Tagger {

	private String pathOut="c:\\a\\";
	private String pathMallet="C:\\Users\\admin\\Downloads\\mallet-2.0.6\\";
	private String topicsFile="keys.txt";
	private String articleTopicFile="topics.txt";
	private String ldaFile="data.mallet";
	private int numOfTopics;
	private int numOfIteration;
	private int numWordsInTopic;

	private BufferedReader reader;
	private HashMap<Integer, Topic> allTopics;

	public MalletTagger(String pathOut, String topicsFile,String articleTopicFile,String ldaFile,String pathMallet,int numOfTopics,int numOfIteration,int numWordsInTopic){
		this.pathOut = pathOut;
		this.topicsFile=topicsFile;
		this.articleTopicFile=articleTopicFile;
		this.ldaFile=ldaFile;
		this.pathMallet=pathMallet;
		this.numOfTopics=numOfTopics;
		this.numOfIteration=numOfIteration;
		this.numWordsInTopic=numWordsInTopic;		
	}

	public void addArticleTopics(Article article,String fileName) {
		
		if (reader==null){
			try {
				reader = new BufferedReader(new FileReader(new File(pathOut+articleTopicFile)));
				reader.readLine();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (allTopics==null)
			allTopics=topicsReader();
		
		try {
			String line=reader.readLine();
			
			if (line==null){
				reader = new BufferedReader(new FileReader(new File(pathOut+articleTopicFile)));
				reader.readLine();
				line=reader.readLine();	
			}
			
			while (true){
				String[] splited = line.split(" ");
				Vector<String> file=String2.split(splited[1],"\\");

				if (file.get(file.size()-1).equals(fileName)){

					String topicId = "";
					String topicDist="";
					for (int i=2; i<splited.length; i+=2){
						topicId=splited[i];
						topicDist=splited[i+1];
						if (Double.parseDouble(topicDist)>=Consts.MIN_DISTRIBUTION | i/2-1<Consts.MIN_TOPICS_IN_ARTICLE){
							article.addTopic(allTopics.get(Integer.parseInt(topicId)));
						}
					}
					return ;
				}

				line=reader.readLine();
				if (line==null){
					reader = new BufferedReader(new FileReader(new File(pathOut+articleTopicFile)));
					reader.readLine();
					line=reader.readLine();	
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public HashMap<Integer, Topic> tagCorpus(String corpusPath) {
		try {
			File f1=new File(pathOut+articleTopicFile);
			File f2=new File(pathOut+topicsFile);
			if (f1.exists())
				f1.delete();
			if (f2.exists())
				f2.delete();			
			Runtime r = Runtime.getRuntime();
			Process p = r.exec("cmd /c "+pathMallet+"bin\\mallet import-dir --input "+corpusPath+" --output "+pathOut+ldaFile+" --keep-sequence --remove-stopwords");
			System.out.println(pathMallet+"bin\\mallet import-dir --input "+corpusPath+" --output "+pathOut+ldaFile+" --keep-sequence --remove-stopwords");
			p.waitFor();
			System.out.println("CmdMallet done 1");//TODO done1 in CmdMallet
			p = r.exec("cmd /c "+pathMallet+"bin\\mallet train-topics --input "+pathOut+ldaFile+" --num-topics "+numOfTopics+" --num-iterations "+numOfIteration+" --output-doc-topics "+pathOut+articleTopicFile+" --num-top-words "+numWordsInTopic+" --output-topic-keys "+pathOut+topicsFile);
			System.out.println(pathMallet+"bin\\mallet train-topics --input "+pathOut+ldaFile+" --num-topics "+numOfTopics+" --num-iterations "+numOfIteration+" --output-doc-topics "+pathOut+articleTopicFile+" --num-top-words "+numWordsInTopic+" --output-topic-keys "+pathOut+topicsFile);//TODO
//			p.waitFor();
			do{
				System.out.println("I am sleeping");//TODO
				Thread.sleep(60000);
			}
			while (!f1.exists() | !f2.exists());
			System.out.println("good morning world");//TODO
			System.out.println("CmdMallet done 2");//TODO done2 in CmdMallet

			reader = new BufferedReader(new FileReader(new File(pathOut+articleTopicFile)));
			reader.readLine();
			allTopics=topicsReader();
			return allTopics;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}


	private static Topic lineToTopic(String line){
		String[] splited=line.split(" ");
		String[] splited2 = splited[0].split(""+(char)9);
		Topic topic=new Topic(Integer.parseInt(splited2[0]));
		topic.addWord(splited2[2]);
		for(int i=1;i<splited.length;i++){
			topic.addWord(splited[i]);
		}
//		System.out.println(topic.getName());//TODO
		return topic;
	}


	public HashMap<Integer,Topic> topicsReader(){
		HashMap<Integer,Topic> ans=new HashMap<Integer,Topic>();
		try {
			BufferedReader reader=new BufferedReader(new FileReader(new File(pathOut+topicsFile)));

			String topicLine=reader.readLine();
			while (topicLine!=null){
				Topic topic=lineToTopic(topicLine);
				ans.put(topic.getId(), topic);

				topicLine=reader.readLine();
			}

			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ans;
	}

	public HashMap<Integer, Topic> getAllTopics(){
		if (allTopics==null)
			allTopics=topicsReader();
		return allTopics;
	}
	
}
